const mysql = require("mysql2/promise");

// 创建 MySQL 连接 pool
const db = mysql.createPool({
  host: process.env.DB_HOST,
  port: process.env.DB_PORT || 3306,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  timezone: "+08:00", // 设置时区为北京时间
  typeCast(field, next) {
    if (
      field.type === "DATETIME" ||
      field.type === "TIMESTAMP" ||
      field.type === "DATE"
    ) {
      return field.string(); // 返回字符串，不转为 UTC Date 对象
    }
    return next();
  },
});

// 测试连接
db.getConnection()
  .then((connection) => {
    console.log("✅ 数据库连接成功");
    connection.release();
  })
  .catch((error) => {
    console.error("❌ 数据库连接失败:", error);
  });
module.exports = db;
